import json
import traceback

from fastapi import APIRouter, Depends
from sqlalchemy import BigInteger

from app.api.response import Response, response_docs
from app.business.session import (
    SessionCreateBiz, SessionUpdateBiz, SessionDeleteBiz, SessionListBiz,
)
from app.api.status import Status
from app.datatype.session import SessionListMdl
from app.initializer import g
from app.middleware.auth import JWTUser, get_current_user

session_router = APIRouter()


# @session_router.get(
#     path="/session/{session_id}",
#     summary="sessionDetail",
#     responses=response_docs(
#         model=SessionDetailBiz,
#     ),
# )
# async def detail(
#         session_id: str,
#         current_user: JWTUser = Depends(get_current_user),
# ):
#     try:
#         session_biz = SessionDetailBiz(id=session_id)
#         data = await session_biz.detail()
#         if not data:
#             return Response.failure(msg="未匹配到记录", status=Status.RECORD_NOT_EXIST_ERROR)
#     except Exception as e:
#         g.logger.error(traceback.format_exc())
#         return Response.failure(msg="sessionDetail失败", error=e)
#     return Response.success(data=data)

@session_router.post(
    path="/createSession",
    summary="创建会话",
    responses=response_docs(data={
        "id": int,
    }),
)
async def create(
        session_biz: SessionCreateBiz,
        current_user: JWTUser = Depends(get_current_user),
):
    try:
        user_id = current_user.id
        session_biz.user_id = user_id
        session_id = await session_biz.create()
        if not session_id:
            return Response.failure(msg="创建失败", status=Status.RECORD_EXISTS_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="创建失败", error=e)
    return Response.success(data={"id": session_id})


@session_router.put(
    path="/updateSession",
    summary="更新会话",
    responses=response_docs(data={
        "id": int,
    }),
)
async def update(
        session_biz: SessionUpdateBiz,
        current_user: JWTUser = Depends(get_current_user),
):
    try:
        session_id = await session_biz.update()
        if not session_id:
            return Response.failure(msg="更新失败", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="更新失败", error=e)
    return Response.success(data={"id": session_id})


@session_router.delete(
    path="/deleteSession",
    summary="删除会话",
    responses=response_docs(data={
        "id": int,
    }),
)
async def delete(
        session_biz: SessionDeleteBiz,
        current_user: JWTUser = Depends(get_current_user)
):
    try:
        session_id = await session_biz.delete()
        if not session_id:
            return Response.failure(msg="删除失败", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="删除失败", error=e)
    return Response.success(data={"id": session_id})


@session_router.get(
    path="/sessionList",
    summary="获取会话列表",
    responses=response_docs(
        model=SessionListMdl,
        is_listwrap=True,
    ))
async def lst(
        current_user: JWTUser = Depends(get_current_user),
):
    try:
        session_biz = SessionListBiz(user_id=current_user.id)
        data = await session_biz.lst()
        if not data:
            return Response.failure(msg="未匹配到记录", status=Status.RECORD_NOT_EXIST_ERROR)
    except Exception as e:
        g.logger.error(traceback.format_exc())
        return Response.failure(msg="查询失败", error=e)
    return Response.success(data= data)
